<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">商品id</label>
        <div class="layui-input-block">
            <input type="text" name="goodsid" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input">
            <tip>填写自己管理账号的名称。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品分类</label>
        <div class="layui-input-block">
            <input type="hidden" name="goodscategoryids">
            <select  name="goodscategoryid" class="layui-select"></select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品名称</label>
        <div class="layui-input-block">
            <input type="text" name="goodsname" placeholder="请输入商品名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品购买积分</label>
        <div class="layui-input-block">
            <input type="text" name="goodsaccpoints" placeholder="商品购买积分" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-upload">
        <label class="layui-form-label">LOGO图片*</label>
        <div class="layui-upload-list">
            <img class="layui-upload-img" id="demo1" width="100px" height="100px">
            <p id="demoText"></p>
        </div>
        <div class="layui-input-block">
            <button type="button" class="layui-btn test1" id="test1">上传图片</button>
            <button class="layui-btn layui-btn-normal" id="AddFile" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
    <!--<div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" id="test1" lay-filter="test1" class="layui-btn layui-btn-normal"></button>
            <button type="button" id="test2" lay-filter="test2" class="layui-btn layui-btn-danger"></button>
            <button type="button" id="test3" lay-filter="test3" class="layui-btn layui-btn-warm"></button>
            &lt;!&ndash;<input class="layui-btn layui-btn-warm" id="btnClose" type="button" value="关闭本页" onClick="custom_close()" />&ndash;&gt;
        </div>
    </div>-->

</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../js/YouJs/config.js"></script>
<script src="../../js/YouJs/reqInterceptor.js"></script>
<script>
    layui.use(['form','upload'], function () {
        var form = layui.form,
            layer = layui.layer,
            upload = layui.upload,
            $ = layui.$;
        $.ajaxSetup({
            // 发送cookie
            xhrFields: {
                withCredentials: true
            },
            // 请求发送前
            beforeSend: function () {
                // 发送请求前，可以对data、url等处理
            },
            // 请求返回
            complete: function () {
                // 返回数据，根据数据调转页面等
            }
        });
        $(function () {
            var goodscategoryids=$("[name=goodscategoryids]").val();
            $.getJSON(config.gatewayUri+"/good/api/goods-category/admin/getAllGoodsCategoryList",function (data) {
                var str = "";
                for (var i = 0; i < data.data.length; i++) {
                    var key = data.data[i];
                    if(key.goodscategoryid==goodscategoryids){
                        str += "<option value='" + key.goodscategoryid + "' selected>" + key.goodscategoryname + "</option>";
                    }else{
                        str += "<option value='" + key.goodscategoryid + "'>" + key.goodscategoryname + "</option>";
                    }
                }
                $("[name=goodscategoryid]").html(str)
                form.render();
            })
            setTimeout(function(){

            },1000)
        })
        //监听提交
        /*form.on('submit(saveBtn)', function (data) {
            var index = layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            }, function () {

                // 关闭弹出层
                layer.close(index);

                var iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);

            });

            return false;
        });*/
        var uploadInst = upload.render({
            elem: '.test1'
            ,url: config.gatewayUri+'/good/api/goods/admin/saveOrUpdateGoods' //改成您自己的上传接口
            ,method:"post"
            ,size:500000   //可以上传的大小1024=1kb
            ,exts:"jpg|png|gif" //可上传的后缀名称
            ,accept:"file" //video audio images
            ,field:"file1" //设置文件域的字段名
            ,auto:false //设置不自动上传
            ,bindAction:"#AddFile" //将 auto设置为false后要设置一个按钮(#fileAdd)可以将图片上传
            ,before: function(obj){
                flag=true;
                this.data={
                    goodsid:$("[name=goodsid]").val(),
                    goodscategoryid:$("[name=goodscategoryid]").val(),
                    goodsname:$("[name=goodsname]").val(),
                    goodsaccpoints:$("[name=goodsaccpoints]").val(),
                    cname:'admin',
                }
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code == "500"){
                    return layer.msg(res.msg);
                }
                //上传成功
                layer.msg(res.msg);
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
    });
</script>
</body>
</html>